{% extends 'configmanager/__base__.html' %}
{% block title %}站点族管理 | {% endblock %}
{% block search_notice %}请输入 站点族简称{% endblock %}
{% block content %}


<div style="display:none;" id="alert-div" class="alert"></div>

<div class="container-fluid btn-group">
    <a href="javascript:void(0);" onclick="RaceAdd('{{ page_obj.number }}')" type="button" class="btn btn-default btn-success">新增站点族</a>
</div><br><br>

<div id="whole" class="container-fluid table-responsive">
    {% if Race_list %}
    <table class="table table-striped table-hover table-condensed">
        <thead>
        <th>站点族编号</th>
        <th>简称</th>
        <th>关联站点</th>
        <th>操作</th>
        </thead>
        <tbody>
        {% for race in Race_list %}
        <tr>
            <td>{{ race.raceid }}</td>
            <td>{{ race.alias }}</td>
            <td>
                {% for site in race.site_set.all %}
                <p>{{ site.fullname }}</p>
                {% endfor %}
            </td>
            <td>
                <a href="{% url 'configmanager:raceedit' race.id %}" type="button" class="btn btn-info">编辑</a>
                <a href="javascript:void(0);" onclick="RaceDelete('{{ race.id }}', '{{ race.alias }}', '{{ page_obj.number }}')" type="button"
                   class="btn btn-danger">删除</a>
            </td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
    {% else %}
    <p>No SiteRace available</p>
    {% endif %}

</div>

<script>
    $.ajaxSetup({
    data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
    });

    //主体刷新
    function WholePartialRefresh(pagenumber, type) {
        var postUrl = "/race/wholerefresh/"+pagenumber+"/"+type+"/";

        $.post(postUrl, function(data) {
            if (data.empty) {
                window.location.replace("/race/?q=&page="+data.pagenumber);
            }
            if (data.add) {
                 window.location.replace("/race/?q=&page="+data.pagenumber);
            }
            else {
                $('#whole').html(data);
            }
        });
    }

    // 新增站点族
    function RaceAdd(pagenumber) {
    var postUrl = "/race/add/";

    jQuery('#body').showLoading();

    $.post(postUrl, function (data) {
        if (data.success) {
            WholePartialRefresh(pagenumber);
            $("#alert-div").css("display", "");
            $("#alert-div").attr("class", "alert alert-success");
            $("#alert-div").text("成功！新增站点族");
            setTimeout(function () {
                $("#alert-div").fadeOut();
            },20000);
        } else {
            alert("操作失败！"+data.message);
        }
        setTimeout(function () {
            jQuery('#body').hideLoading();
        },300);
    });
    }

    // 删除站点族
    function RaceDelete(race_id, race_alias, pagenumber) {
    var postUrl = "/race/"+race_id+"/delete/";
    var race_alias = race_alias;
    var type = "delete"

    target = $("#myspin").get(0);
    var bgDiv = document.getElementById("bgDiv");
    bgDiv.style.display = "block";
    spinner.spin(target);

    $.post(postUrl, function (data) {
        if (data.success) {
            WholePartialRefresh(pagenumber,type);
            $("#alert-div").attr("class", "alert alert-danger");
            $("#alert-div").text("成功！删除站点族 "+race_alias);
            $("#alert-div").css("display", "");
            setTimeout(function () {
                $("#alert-div").fadeOut();
            },20000);
        } else {
            alert("操作失败！"+data.message);
        }
        setTimeout(function () {
            spinner.spin();
            bgDiv.style.display = "none";
        },300);
    });
    }


</script>
{% endblock %}